﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCT.CodeBox.EventTrigger.Codes;
using System.Threading;

namespace DCT.CodeBox.EventTrigger
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();

            Console.ReadLine();
        }

        private static void Test1() {
            Cat cat = new Cat();
            Mouse m1 = new Mouse("老鼠1");
            Mouse m2 = new Mouse("老鼠2");
            Mouse m3 = new Mouse("老鼠3");
            Mouse m4 = new Mouse("老鼠4");

            cat.EventMiaow.Add(delegate { m1.Run(); return true; });
            cat.EventMiaow.Add(delegate { m2.Run(); return true; });
            cat.EventMiaow.Add(delegate { m3.Run(); return true; });
            cat.EventMiaow.Add(delegate { m4.Run(); return true; });

            cat.Miaow();

            Console.WriteLine("=====移除监听“老鼠3、4”");
            cat.EventMiaow.RemoveAt(3);
            cat.EventMiaow.RemoveAt(2);
            cat.Miaow();

            Console.WriteLine("=====清除所有监听");
            cat.EventMiaow.Clear();
            cat.Miaow();

            Console.WriteLine("=====测试异步Fire=====");
            cat.EventMiaow.Clear();
            cat.EventMiaow.Add(delegate { m1.RunDelay(10); return true; });
            cat.EventMiaow.Add(delegate { m2.RunDelay(5); return true; });
            cat.EventMiaow.Add(delegate { m3.RunDelay(1); return true; });
            cat.EventMiaow.Add(delegate { m4.RunDelay(2); return true; });
            //因为异步Fire，所以只需要10秒左右就完成（m1最长Delay10s）
            cat.InvokeMiaow();
        }

        private static void Test2() {
            //RawCat cat = new RawCat();
            //Mouse m1 = new Mouse("老鼠1");
            //Mouse m2 = new Mouse("老鼠2");
            //Mouse m3 = new Mouse("老鼠3");
            //Mouse m4 = new Mouse("老鼠4");
            //cat.MiaowHandler += new EventHandler(cat_MiaowHandler);
            //EventProxy ep = new EventProxy(cat, "MiaowHandler");
            //ep.Listiner.Add(delegate { m1.Run(); return true; });
            //ep.Listiner.Add(delegate { m1.Run(); return true; });
            //ep.Listiner.Add(delegate { m1.Run(); return true; });
            //ep.Listiner.Add(delegate { m1.Run(); return true; });

            //cat.Miaow();
        }

        static void cat_MiaowHandler(object sender, EventArgs e)
        {
            
        }
    }

    #region Defined
    /// <summary>
    /// 猫咪
    /// </summary>
    public class Cat
    {
        /// <summary>
        /// 定义事件
        /// </summary>
        private EventTrigger<Cat> miaowTrigger = EventTrigger<Cat>.CreateInstance();

        /// <summary>
        /// 公开监听
        /// </summary>
        public EventTriggerListener<Cat> EventMiaow
        {
            get
            {
                return this.miaowTrigger.OutsideListener;
            }
        }

        /// <summary>
        /// 猫咪叫
        /// </summary>
        public void Miaow()
        {
            Console.WriteLine(DateTime.Now.ToString() + ":" + "猫咪叫啦~~");
            this.miaowTrigger.Fire(this);
        }

        /// <summary>
        /// 猫咪叫，异步执行
        /// </summary>
        public void InvokeMiaow()
        {
            Console.WriteLine(DateTime.Now.ToString() + ":" + "猫咪异步叫啦~~");
            this.miaowTrigger.InvokeFire(this);
        }
    }

    public class Mouse
    {
        public string Name { get; private set; }

        public Mouse(string name)
        {
            this.Name = name;
        }

        public void Run()
        {
            Console.WriteLine(DateTime.Now.ToString() + ":" + this.Name + "跑掉了...");
        }

        public void RunDelay(int second)
        {
            Thread.Sleep(second * 1000);
            Console.WriteLine(DateTime.Now.ToString() + ":" + this.Name + "跑掉了...用了" + second.ToString() + "秒");
        }
    }

    public class RawCat
    {
        public event EventHandler MiaowHandler;

        public void Miaow()
        {
            Console.WriteLine(DateTime.Now.ToString() + ":" + "猫咪叫啦~~");
            if (MiaowHandler != null)
            {
                MiaowHandler(this, EventArgs.Empty);
            }
        }
    }
    #endregion
    
}
